﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE
Imports SenseSDKExtended.DrawingCEEx

#End Region

Namespace DrawingCEEx.SenseAPIsEx.SenseInputBox

    <System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
    Friend Class HTCStyle3Modal
        Inherits StedySoft.SenseSDK.ITranslucentForm

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        Private _titleText As String = Nothing
        Private _promptText As String = Nothing
        Private _selectedValue As String = Nothing
        Private _items As ArrayList = Nothing

        Private _topImage As IImage = _getIImageFromResource("inputbox.top")
        Private _midImage As IImage = _getIImageFromResource("inputbox.middle")
        Private _botImage As IImage = _getIImageFromResource("inputbox.bottom")

        Private _topImageRectangle As Rectangle = Nothing
        Private _midImageRectangle As Rectangle = Nothing
        Private _botImageRectangle As Rectangle = Nothing

        Private _titleTextRectangle As Rectangle = Nothing
        Private _promptTextRectangle As Rectangle = Nothing

        Private _valueComboBoxRectangle As Rectangle = Nothing
        Private _okButtonRectangle As Rectangle = Nothing
        Private _cancelButtonRectangle As Rectangle = Nothing

        Private _totalHeight As Integer = Nothing
        Private _middleHeight As Integer = Nothing

        Private WithEvents _valueComboBox As SenseComboControl = New SenseComboControl
        Private components As System.ComponentModel.IContainer

        Private Sub SenseInputBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

            _totalHeight = SenseAPIs.SenseImage.GetIImageSize(_topImage).Height + If(_isVGA, 100, 50) + SenseAPIs.SenseImage.GetIImageSize(_botImage).Height
            _middleHeight = SenseAPIsEx.SenseStrings.CalculateStringHeight(_promptText, "Tahoma", 8, FontStyle.Regular, New Size(SenseAPIs.SenseImage.GetIImageSize(_midImage).Width - If(_isVGA, 20, 10), Nothing)) + If(_isVGA, 100, 50) + If(_isVGA, 20, 10)

            _topImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_topImage).Width) / 2), ClientRectangle.Top + If(_isVGA, 20, 10), SenseAPIs.SenseImage.GetIImageSize(_topImage).Width, SenseAPIs.SenseImage.GetIImageSize(_topImage).Height)
            _midImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_midImage).Width) / 2), _topImageRectangle.Top + _topImageRectangle.Height, SenseAPIs.SenseImage.GetIImageSize(_midImage).Width, _middleHeight)
            _botImageRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - SenseAPIs.SenseImage.GetIImageSize(_botImage).Width) / 2), _midImageRectangle.Top + _midImageRectangle.Height, SenseAPIs.SenseImage.GetIImageSize(_botImage).Width, SenseAPIs.SenseImage.GetIImageSize(_botImage).Height)

            _titleTextRectangle = New Rectangle(_topImageRectangle.Left + If(_isVGA, 40, 20), _topImageRectangle.Top + If(_isVGA, 8, 4), SenseAPIs.SenseImage.GetIImageSize(_topImage).Width - If(_isVGA, 20, 10), SenseAPIs.SenseImage.GetIImageSize(_topImage).Height)
            _promptTextRectangle = New Rectangle(_topImageRectangle.Left + If(_isVGA, 40, 20), _midImageRectangle.Top + If(_isVGA, 10, 5), SenseAPIs.SenseImage.GetIImageSize(_midImage).Width - If(_isVGA, 40, 20) - (_topImageRectangle.Left + If(_isVGA, 40, 20)), SenseAPIsEx.SenseStrings.CalculateStringHeight(_promptText, "Tahoma", 8, FontStyle.Regular, New Size(SenseAPIs.SenseImage.GetIImageSize(_midImage).Width - If(_isVGA, 40, 20) - (_topImageRectangle.Left + If(_isVGA, 40, 20)), Nothing)))

            _valueComboBoxRectangle = New Rectangle(_midImageRectangle.Left + If(_isVGA, 40, 20), _promptTextRectangle.Top + _promptTextRectangle.Height + If(_isVGA, 20, 10), SenseAPIs.SenseImage.GetIImageSize(_topImage).Width - (_midImageRectangle.Left + If(_isVGA, 40, 20)) - If(_isVGA, 40, 20), If(_isVGA, 40, 20))
            _okButtonRectangle = New Rectangle(CInt((Screen.PrimaryScreen.Bounds.Width - _botImageRectangle.Width) / 2) + CInt(((_botImageRectangle.Width / 2) - If(_isVGA, 174, 87)) / 2) + If(_isVGA, 8, 4), (_botImageRectangle.Top - If(_isVGA, 8, 4)) + CInt((_botImageRectangle.Height - If(_isVGA, 79, 40)) / 2), If(_isVGA, 174, 87), If(_isVGA, 79, 40))
            _cancelButtonRectangle = New Rectangle(Screen.PrimaryScreen.Bounds.Width - _okButtonRectangle.Left - _okButtonRectangle.Width, (_botImageRectangle.Top - If(_isVGA, 8, 4)) + CInt((_botImageRectangle.Height - If(_isVGA, 79, 40)) / 2), If(_isVGA, 174, 87), If(_isVGA, 79, 40))

            Dim _ok = New SenseButtonControlEx.HTCButtonStyle1
            _ok.Name = "_ok"
            _ok.Text = "Ok"
            _ok.Left = _okButtonRectangle.Left
            _ok.Top = _okButtonRectangle.Top
            _ok.Width = _okButtonRectangle.Width
            _ok.Height = _okButtonRectangle.Height
            _ok.BackColor = Color.White
            AddHandler _ok.OnClick, AddressOf _ok_OnClick
            Me.Controls.Add(_ok)

            Dim _cancel = New SenseButtonControlEx.HTCButtonStyle1
            _cancel.Name = "_cancel"
            _cancel.Text = "Cancel"
            _cancel.Left = _cancelButtonRectangle.Left
            _cancel.Top = _cancelButtonRectangle.Top
            _cancel.Width = _cancelButtonRectangle.Width
            _cancel.Height = _cancelButtonRectangle.Height
            _cancel.BackColor = Color.White
            AddHandler _cancel.OnClick, AddressOf _cancel_OnClick
            Me.Controls.Add(_cancel)

            _valueComboBox.Width = _valueComboBoxRectangle.Width
            _valueComboBox.Top = _valueComboBoxRectangle.Top
            _valueComboBox.Left = _valueComboBoxRectangle.Left
            _valueComboBox.Height = _valueComboBoxRectangle.Height

            For Count As Integer = 0 To _items.Count - 1
                _valueComboBox.Items.Add(New SenseComboControl.Item(_items.Item(Count).ToString.Substring(0, _items.Item(Count).ToString.IndexOf(",")), _items.Item(Count).ToString.Substring(_items.Item(Count).ToString.IndexOf(","), _items.Item(Count).ToString.Length - _items.Item(Count).ToString.IndexOf(",")).TrimStart(CChar(","))))
            Next

            _valueComboBox.SelectedValue = _selectedValue

            AddHandler _valueComboBox.OnSelectedValueChanged, AddressOf valueComboBox_OnSelectedValueChanged

            Me.Controls.Add(_valueComboBox)

        End Sub

        Public Sub valueComboBox_OnSelectedValueChanged(ByVal sender As Object, ByVal value As Object)

            _selectedValue = _valueComboBox.SelectedValue.ToString

        End Sub

        Private Sub _ok_OnClick(ByVal sender As Object)

            Me.Close()

        End Sub

        Private Sub _cancel_OnClick(ByVal sender As Object)

            Me.Close()

        End Sub

        Private Sub SenseInputBox_OnRender(ByVal g As System.Drawing.Graphics) Handles MyBase.OnRender

            g.DrawImageAlphaChannel(_topImage, New Rectangle(_topImageRectangle.Left, _topImageRectangle.Top, _topImageRectangle.Width, _topImageRectangle.Height))

            g.DrawImageAlphaChannel(_midImage, New Rectangle(_midImageRectangle.Left, _midImageRectangle.Top, _midImageRectangle.Width, _midImageRectangle.Height))

            g.DrawImageAlphaChannel(_botImage, New Rectangle(_botImageRectangle.Left, _botImageRectangle.Top, _botImageRectangle.Width, _botImageRectangle.Height))

            'Draw the title text with other properties like height,color,style,alignment,etc
            Using f As Font = New Font("Tahoma", 9, FontStyle.Regular)
                Dim s As String = SenseAPIsEx.SenseStrings.GetTrimmedDottedText(_titleText, "Tahoma", 9, FontStyle.Regular, New Size(_topImageRectangle.Width, _topImageRectangle.Height), g)
                Dim r As Rectangle = New Rectangle(_titleTextRectangle.Left, _titleTextRectangle.Top, _titleTextRectangle.Width, _titleTextRectangle.Height) '(Left,Top,Width,Height)
                Using b As Brush = New SolidBrush(Color.White)
                    Using sf As StringFormat = New StringFormat()
                        sf.FormatFlags = StringFormatFlags.NoWrap
                        sf.Alignment = StringAlignment.Near
                        sf.LineAlignment = StringAlignment.Center
                        g.DrawString(s, f, b, r, sf)
                    End Using
                End Using
            End Using

            'Draw the prompt text with other properties like height,color,style,alignment,etc
            Using f As Font = New Font("Tahoma", 8, FontStyle.Regular)
                Dim s As String = _promptText
                Dim r As Rectangle = New Rectangle(_promptTextRectangle.Left, _promptTextRectangle.Top, _promptTextRectangle.Width, _promptTextRectangle.Height) '(Left,Top,Width,Height)
                Using b As Brush = New SolidBrush(Color.Black)
                    Using sf As StringFormat = New StringFormat()
                        sf.Alignment = StringAlignment.Near
                        g.DrawString(s, f, b, r, sf)
                    End Using
                End Using
            End Using

        End Sub

        Public Property Title() As String
            Get
                Return _titleText
            End Get
            Set(ByVal value As String)
                _titleText = value
            End Set
        End Property

        Public Property Prompt() As String
            Get
                Return _promptText
            End Get
            Set(ByVal value As String)
                _promptText = value
            End Set
        End Property

        Public Property SelectedValue() As String
            Get
                Return _selectedValue
            End Get
            Set(ByVal value As String)
                _selectedValue = value
            End Set
        End Property

        Public Property Items() As ArrayList
            Get
                Return _items
            End Get
            Set(ByVal value As ArrayList)
                _items = value
            End Set
        End Property

        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.SuspendLayout()
            '
            'HTCStyle1Modal
            '
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit
            Me.ClientSize = New System.Drawing.Size(240, 294)
            Me.Name = "HTCStyle3Modal"
            Me.ResumeLayout(False)

        End Sub




    End Class

End Namespace

